﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web.backend
{
    public partial class articleList : BasePage
    {
        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BuildTheParentCategory();
                ddlParentCategory_SelectedIndexChanged(null, null);
                BindGrid();
            }
        }

        private void BuildTheParentCategory()
        {
            this.ddlParentCategory.Items.Clear();
            var parent = contex.categories.Where(p => p.art_parent_kind == 0).ToList();
            foreach (category cat in parent)
            {
                this.ddlParentCategory.Items.Add(new ListItem(cat.art_kind_name, cat.art_kind_id.ToString()));
            }
            this.ddlParentCategory.Items.Insert(0, new ListItem("--请选择--", "-1"));
        }

        protected void ddlParentCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlCategory.Items.Clear();
            int pid = Utility.ConvertIntFromString(this.ddlParentCategory.SelectedItem.Value);
            var cat = from c in contex.categories where c.art_parent_kind == pid select c;
            List<category> cates = cat.ToList<category>();
            foreach (category item in cates)
            {
                ListItem dropItem = new ListItem(item.art_kind_name, item.art_kind_id.ToString());
                this.ddlCategory.Items.Add(dropItem);
            }
            this.ddlCategory.Items.Insert(0, new ListItem("--请选择--", "-1"));
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            var list = from c in contex.articles orderby c.update_date descending select c;
            var query = list.AsQueryable();

            if (this.txtTitle.Text.Trim() != "")
            {
                query = query.Where(p => p.article_title.Contains(this.txtTitle.Text.Trim()));
            }
            if (this.ddlParentCategory.SelectedItem.Value != "-1")
            {
                int pid = int.Parse(this.ddlParentCategory.SelectedItem.Value);
                query = query.Where(p => p.parent_category_id == pid);
            }
            if (this.ddlCategory.SelectedItem.Value != "-1")
            {
                int cid = int.Parse(this.ddlCategory.SelectedItem.Value);
                query = query.Where(p => p.category_id == cid);
            }

            List<article> articles = query.ToList();

            AspNetPager1.RecordCount = articles.Count;

            psd.DataSource = articles;
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptCategory.DataSource = psd;
            this.rptCategory.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                article cat = item as article;
                Label title = e.Item.FindControl("lblTitle") as Label;
                Label recordNo = e.Item.FindControl("lblNo") as Label;
                LinkButton button = e.Item.FindControl("btnDelete") as LinkButton;
                if (cat.article_title.Length >= 35)
                {
                    title.Text = cat.article_title.Substring(0, 35) + "...";
                }
                else
                {
                    title.Text = cat.article_title;
                }
                title.ToolTip = cat.article_title;
                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                button.Attributes.Add("onclick", "return confirm('您确定要删除该文章吗');");
            }

        }

        protected void rptCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
                int category_id = Convert.ToInt32(e.CommandArgument);
                article cat = contex.articles.SingleOrDefault(p => p.article_id == category_id);
                if (cat != null)
                {
                    contex.articles.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                    if (!string.IsNullOrEmpty(cat.attachment_path))
                    {
                        string filePath = Server.MapPath("~/") + cat.attachment_path;
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                    }

                    if (!string.IsNullOrEmpty(cat.article_title_image))
                    {
                        string filePath = Server.MapPath("~/") + cat.article_title_image;
                        string thumbFilePath = Server.MapPath("~/") + cat.article_title_image.Replace("thumb_","");
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        if (System.IO.File.Exists(thumbFilePath))
                        {
                            System.IO.File.Delete(thumbFilePath);
                        }
                    }
                }
                BindGrid();
            }
        }
    }
}